import { storage } from "@/utils/storage";
import { PageEnum } from "@/enums/pageEnum";
import router from "@/router";
import { checkStatus } from "./checkStatus";

const { BASE_LOGIN } = PageEnum;

export interface ResponseCode {
  code: number;
  msg: string;
  data?: any;
}

export const handleResponseCode = (response: ResponseCode) => {
  const { code, msg } = response;

  switch (code) {
    case 200:
      return { success: true, msg };

    case 401: // 未授权
      checkStatus(code, msg);
      storage.clear();
      router.replace({
        path: BASE_LOGIN,
        query: {
          redirect: router.currentRoute.value.fullPath,
        },
      });
      return { success: false, msg };

    default:
      checkStatus(code, msg);
      return { success: false, msg };
  }
};
